1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import axios from 'axios';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import Seo from '@/core/components/Seo';
import dynamic from 'next/dynamic';
import { getNameFromSlug } from '@/core/utils/slug';
import { capitalizeEachWord } from '../../utils/capializeFIrstWord';
const BasicLayout = dynamic(() =>
import('@/core/components/layouts/BasicLayout')
);
const ProductSearch = dynamic(() =>
import('@/lib/product/components/ProductSearch')
);
export default function FindPage() {
const route = useRouter();
const [result, setResult] = useState(null);
const [query, setQuery] = useState(null);
const slugRaw = route.query.slug || null;
console.log(slugRaw);
// const cleanKey = slugRaw ? getNameFromSlug(slugRaw) : '';
// console.log(cleanKey);
const readableSlug = capitalizeEachWord(slugRaw);
const getSearchKeyData = async (clean) => {
try {
const res = await axios(
`${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/searchkey?url=${clean}&from=searchkey`
);
setResult(res?.data?.response?.docs?.[0] || null);
} catch (e) {
console.error('Fetching searchkey failed:', e);
}
};
useEffect(() => {
if (!route.isReady) return;
if (!slugRaw) return;
getSearchKeyData(slugRaw);
}, [route.isReady, slugRaw]);
useEffect(() => {
if (result) {
const ids = result.product_ids_is || [];
setQuery({
ids: ids.join(','),
from: 'searchkey',
});
}
}, [result]);
return (
<BasicLayout>
<Seo
title={`Beli ${readableSlug} Original & Harga Terjangkau - indoteknik.com`}
description={`Beli ${readableSlug} Kirim Jakarta Surabaya Semarang Makassar Manado Denpasar.`}
additionalMetaTags={[
{
property: 'keywords',
content: `Beli ${readableSlug}, harga ${readableSlug}, ${readableSlug} murah`,
},
]}
canonical={`${process.env.NEXT_PUBLIC_SELF_HOST}${route.asPath}`}
/>
{query && <ProductSearch query={query} prefixUrl={route.asPath} />}
</BasicLayout>
);
}
|